-- UILogin
-- Create by zouyb
-- 聊天界面

Channel_ID_Team = 0;
Channel_ID_Privacy = 1;
Channel_ID_Faction = 2;
Channel_ID_World = 3;
Channel_ID_System = 4;
Channel_ID_Bugle = 5;

--UIChat继承自Layer
UIChat = class("UIChat", function() 
    return cc.Layer:create();
end);

function UIChat.create()
    return UIChat.new();
end    


--构造函数
function UIChat:ctor()
    self:initRichEdit();
end

function UIChat:initRichEdit()    
    --创建小喇叭控件
    self._richBugle = lbs.RichTextUI:create()
    self._richBugle:setContentSize(cc.size(400, 35))
    self._richBugle:setAnchorPoint(cc.p(0, 0))
    self._richBugle:setPosition(cc.p(300, 300))
    self._richBugle:setMaxLine(1)
    --创建聊天控件
    self._richChat= lbs.RichTextUI:create()
    self._richChat:setContentSize(cc.size(400, 300))
    self._richChat:setAnchorPoint(cc.p(0, 0))
    self._richChat:setPosition(cc.p(200, 370))  

    local s = tolua.cast(self._richBugle, "cc.Node")
    self:addChild(s);
    self:addChild(tolua.cast(self._richChat, "cc.Node"));
end

function UIChat:addChatMsg(channel, roleName, chatMsg, signs)
    local richText = (channel == Channel_ID_Bugle) and self._richBugle or self._richChat
    if richText and channel and roleName and chatMsg then
        local ChannelNameSwitch = 
        {
            [Channel_ID_Team] = "【队伍】",
            [Channel_ID_Privacy] = "【私聊】",
            [Channel_ID_Faction] = "【帮会】",
            [Channel_ID_World] = "【世界】",
            [Channel_ID_System] = "【系统】"
        }
        local ChannelColor = 
        {
                [Channel_ID_Team] = cc.c3b(255, 127, 0),
                [Channel_ID_Privacy] = cc.c3b(255, 127, 0),
                [Channel_ID_Faction] = cc.c3b(255, 127, 0),
                [Channel_ID_World] = cc.c3b(255, 127, 0),
                [Channel_ID_System] = cc.c3b(255, 255, 255),
                [Channel_ID_Bugle] = cc.c3b(255, 127, 0)
        }
        local linkColor = cc.c4b(255, 255, 0, 255)
        local linklineColor = cc.c4b(255, 255, 0, 255)   
        local outlineColor = cc.c3b(0, 0, 0, 255) 

        if channel == Channel_ID_Bugle then
            richText:insertNewLine()
        end
        if ChannelNameSwitch[channel] then
            local rc = lbs.RichItemText:create(channel, ChannelColor[channel], 255, (ChannelNameSwitch[channel]), "DFYuanW7-GB2312.ttf", 25)    
            rc:enableOutLine(outlineColor, 2)
            richText:insertElement(rc)
        end
        if channel ~= Channel_ID_System then
            local rcn = lbs.RichItemText:create(channel, linkColor, 255, roleName, "DFYuanW7-GB2312.ttf", 25)  
            rcn:enableLinkLine(linklineColor, 1)
            rcn:enableOutLine(outlineColor, 2)
            richText:insertElement(rcn)
            chatMsg = ":" .. chatMsg
        end
        local rcm = lbs.RichItemText:create(channel, ChannelColor[channel], 255, (chatMsg), "DFYuanW7-GB2312.ttf", 25)  
        richText:insertElement(rcm)
        if channel ~= Channel_ID_Bugle then
            richText:insertNewLine()
        end
    end
end

function UIChat:init()   
    self:addChatMsg(Channel_ID_Bugle, "王小二", "This is Bugle Msg")
    self:addChatMsg(Channel_ID_System, "", "This is System Msg")
    self:addChatMsg(Channel_ID_Team, "王小二", "This is Team Msg")
    self:addChatMsg(Channel_ID_World, "王小二", "This is World Msg")
    self:addChatMsg(Channel_ID_Faction, "王小二", "This is Faction Msg") 
end